package com.lanou.musicbox.adapter.onlinemusic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.RecommentSongsMenu;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/30.
 */
public class RemmendGridViewAdapter extends BaseAdapter {
    private ImageLoader imageLoader;
    private Context context;
    private ArrayList<RecommentSongsMenu.ContentEntity.ListEntity> data;
    private RecommentSongsMenu.ContentEntity.ListEntity entity;

    public RemmendGridViewAdapter(Context context, ArrayList<RecommentSongsMenu.ContentEntity.ListEntity> data) {
        this.context = context;
        this.data = data;
        imageLoader = VolleySingleton.getInstance(context).getImageLoader();

    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /***
     * covertView指的时每个行布局
     * position行不局下标
     * parent列表视图对象
     **/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            //通过布局加载器布局
            convertView = LayoutInflater.from(context).inflate(R.layout.recommend_item, null);
            viewHolder = new ViewHolder();
            viewHolder.recommend_pic = (ImageView) convertView.findViewById(R.id.recommend_pic);
            viewHolder.recommendnum = (TextView) convertView.findViewById(R.id.recommendnum);
            viewHolder.recommendtitle = (TextView) convertView.findViewById(R.id.recommendtitle);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        entity = (RecommentSongsMenu.ContentEntity.ListEntity) getItem(position);
        viewHolder.recommendnum.setText(entity.getListenum());
        viewHolder.recommendtitle.setText(entity.getTitle());
        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(viewHolder.recommend_pic, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(entity.getPic(), imageListener);
        return convertView;
    }

    class ViewHolder {
        private ImageView recommend_pic;
        private TextView recommendnum;
        private TextView recommendtitle;
    }
}
